package com.recyclerview430yjy.model;

import com.recyclerview430yjy.bean.ArticleBean;
import com.recyclerview430yjy.iface.ArticalListener;
import com.recyclerview430yjy.iface.Articaliface;
import com.recyclerview430yjy.service.ArticleService;

import java.util.List;


import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by acer on 2017/5/16.
 */

public class ArticleModel implements Articaliface {
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    public ArticleModel() {
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getReultList(String mod, int page, String sessionId, final ArticalListener listener) {
        ArticleService articleService=retrofit.create((ArticleService.class));
        Call<List<ArticleBean>> call=articleService.getArticleList(mod,page,sessionId);
        call.enqueue(new Callback<List<ArticleBean>>() {
            @Override
            public void onResponse(Call<List<ArticleBean>> call, Response<List<ArticleBean>> response) {
                if(response.isSuccessful()&&response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
                else
                {
                    listener.onFaile("FAIL");
                }

            }

            @Override
            public void onFailure(Call<List<ArticleBean>> call, Throwable t) {
                listener.onFaile(t.toString());

            }
        });

    }
}
